/********************************************************************
  A J A X / J S O N
*******************************************************************/
$.ajaxSetup({
      "error":function(x) { alert("Error:" + x.responseText);  }
});
function submitJsonRest(obj, modelName,  action, callbackFunction, updateUrlFunction) {
	var inputJSON = null;
	var submitUrl = "/dispatch/"+modelName+"/"+action;
	if (this.document.location.href.indexOf("admin") > 0 ) {
		submitUrl = "/admin" + submitUrl;
	}
	if (obj) {
		if (action == "create" || action == "update") {
			inputJSON = $.toJSON(obj);
			//console.log("inputJSON:" + inputJSON);
		}
		if (obj.key) {
			submitUrl += "/"+obj.key;
			//console.log("submitURL:"+submitUrl);
		}
	}
	submitUrl += ".json";
	//console.log("submitURL:"+submitUrl);
	if (updateUrlFunction) {
		submitUrl= updateUrlFunction(submitUrl);
	}
	$.ajax({
  		type: 'POST',
  		url: submitUrl,
  		data: inputJSON,
  		success: function(data) {
			jQuery.container(data.key, data);
			if (callbackFunction) {
				callbackFunction(data);
			};
		},
  		dataType: 'json'
	});
	/*

	$.getJSON(submitUrl, inputJSON, function(data) {
		jQuery.container(data.key, data);
		if (callbackFunction) {
			callbackFunction(data);
		}
	});	
	*/
}
